#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"

#define LED_RED GPIO_PIN_1
#define LED_BLUE GPIO_PIN_2
#define LED_GREEN GPIO_PIN_3

#define BUTTON_1 GPIO_PIN_0
#define BUTTON_2 GPIO_PIN_4

void main(void) {
        int light;

        // Unlock PF0 so we can change it to a GPIO input
        // Once we have enabled (unlocked) the commit register then re-lock it
        // to prevent further changes.  PF0 is muxed with NMI thus a special case.
        HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD;
        HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01;
        HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0;

        // configure system clock to run at 50 MHz
        // use external crystal (16 MHz) and PLL
        SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
        SYSCTL_OSC_MAIN);

        // Enable PORT F GPIO
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

        // set LED pins as outputs
        GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_RED|LED_BLUE|LED_GREEN);

        // SEE COMMENTS IN THE POST

        // set user switches as inputs
        GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, BUTTON_1|BUTTON_2);

        // turn weak pull-ups on
        GPIOPadConfigSet(GPIO_PORTF_BASE, BUTTON_1|BUTTON_2, GPIO_STRENGTH_2MA,
        GPIO_PIN_TYPE_STD_WPU);

        // loop forever
        for (;;) {
                light = 0;

                if(!GPIOPinRead(GPIO_PORTF_BASE, BUTTON_1))
                        light |= LED_RED;

                if(!GPIOPinRead(GPIO_PORTF_BASE, BUTTON_2))
                        light |= LED_BLUE;

                GPIOPinWrite(GPIO_PORTF_BASE, LED_RED|LED_BLUE|LED_GREEN, light);

                SysCtlDelay(500000);
        }
}
